package util

import (
	"context"
	"github.com/go-session/redis"
	"github.com/go-session/session"
	"github.com/mix-go/dotenv"
	_ "mytest/dotenv"
	"net/http"
)

func init() {
	sessionStore := dotenv.Getenv("CAPTCHA_STORE").String()
	if sessionStore == "REDIS" {
		session.InitManager(
			session.SetStore(redis.NewRedisStore(&redis.Options{
				Addr:     dotenv.Getenv("REDIS_ADDR").String(),         // redis server的地址
				Password: dotenv.Getenv("REDIS_PASSWORD").String(),     // redis的密码
				DB:       int(dotenv.Getenv("REDIS_DATABASE").Int64()), // redis的库
			})),
		)
	}
}

func SessionStart(w http.ResponseWriter, r *http.Request) (store session.Store, err error) {
	store, err = session.Start(context.Background(), w, r)
	return
}
